package com.cjc.sport.controller.admin;

import com.cjc.sport.entity.Category;
import com.cjc.sport.entity.EnumValidation;
import com.cjc.sport.entity.NumEnum;
import com.cjc.sport.entity.ReturnJson;
import com.cjc.sport.exception.ServiceException;
import com.cjc.sport.service.CategoryService;
import com.cjc.sport.util.SportUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.ValidationException;
import java.util.List;

/**
 * 管理课程分类表控制层
 *
 * @Author: Bruse Queen
 * @Date: 2018/2/28
 */
@RestController
@CrossOrigin
public class CategoryController {
    /**
     * 日志配置
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(CategoryController.class);
    /**
     * 装配课程分类表服务层
     */
    @Autowired
    private CategoryService categoryService;
    /**
     * 定义返回对象名
     */
    private ReturnJson result;

    /**
     * 新增课程分类
     *
     * @param category 课程分类对象
     * @return ReturnJson 返回封装的Json对象
     */
    @RequestMapping(value = "/admin/addCategory", method = RequestMethod.POST)
    public ReturnJson addCategory(@RequestBody Category category) {
        try {
            // 课程分类是否合法
            if (SportUtils.judgeNull(category.getCategoryName().trim())
                    || category.getCategoryName().length() > EnumValidation.TEN.getNum()) {
                throw new ValidationException("请输入合法的课程分类！");
            }
            // 规整
            category.setCategoryName(category.getCategoryName().trim());
            // 执行插入
            categoryService.addCategory(category);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "保存数据成功！");

        } catch (ValidationException v) {
            LOGGER.error("CategoryController--addCategory error!");
            result = new ReturnJson(NumEnum.FAIL.getIndex(), v.getMessage());
        } catch (ServiceException s) {
            LOGGER.error("CategoryController--addCategory error!");
            result = new ReturnJson(NumEnum.FAIL.getIndex(), s.getMessage());
        } catch (Exception e) {
            LOGGER.error("CategoryController--addCategory error!");
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        }
        return result;
    }

    /**
     * 编辑课程分类
     *
     * @param category 课程分类对象
     * @return ReturnJson 返回封装的Json对象
     */
    @RequestMapping(value = "/admin/updateCategory", method = RequestMethod.POST)
    public ReturnJson updateCategory(@RequestBody @Valid Category category) {
        try {
            // 规整
            category.setCategoryId(category.getCategoryId().trim());
            category.setCategoryName(category.getCategoryName().trim());
            // 执行编辑
            categoryService.updateCategory(category);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "保存数据成功！");

        } catch (ServiceException s) {
            LOGGER.error("CategoryController--updateCategory error!");
            result = new ReturnJson(NumEnum.FAIL.getIndex(), s.getMessage());
        } catch (Exception e) {
            LOGGER.error("CategoryController--updateCategory error!");
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        }
        return result;
    }

    /**
     * 删除课程分类
     *
     * @param category 课程分类对象
     * @return ReturnJson 返回封装的Json对象
     */
    @RequestMapping(value = "/admin/deleteCategory", method = RequestMethod.POST)
    public ReturnJson deleteCategory(@RequestBody Category category) {
        try {
            // 校验字段
            if (SportUtils.judgeNull(category.getCategoryId().trim())
                    || category.getCategoryId().length() > EnumValidation.FOURTY.getNum()) {
                throw new ValidationException("请输入合法的课程分类ID!");
            }
            // 规整
            category.setCategoryId(category.getCategoryId().trim());
            // 执行删除
            categoryService.deleteCategory(category);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "删除数据成功！");

        } catch (ValidationException v) {
            LOGGER.error("CategoryController--deleteCategory error!");
            result = new ReturnJson(NumEnum.FAIL.getIndex(), v.getMessage());
        } catch (ServiceException s) {
            LOGGER.error("CategoryController--deleteCategory error!");
            result = new ReturnJson(NumEnum.FAIL.getIndex(), s.getMessage());
        } catch (Exception e) {
            LOGGER.error("CategoryController--deleteCategory error!");
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        }
        return result;
    }

    /**
     * 获得所有课程分类
     *
     * @return ReturnJson 返回封装的Json对象
     */
    @RequestMapping(value = "/admin/getAllCategories", method = RequestMethod.POST)
    public ReturnJson getAllCategories() {
        try {
            // 执行查询
            List<Category> categories = categoryService.getAllCategories();
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "查询成功!", categories);

        } catch (ServiceException s) {
            LOGGER.error("CategoryController--getAllCategories error!");
            result = new ReturnJson(NumEnum.FAIL.getIndex(), s.getMessage());
        } catch (Exception e) {
            LOGGER.error("CategoryController--getAllCategories error!");
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        }
        return result;
    }
}
